//
//  NIBatteryView.m
//  gweAD
//
//  Created by navinfo air on 2016/11/8.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NIBatteryView.h"

@implementation NIBatteryView{
    __weak IBOutlet UIView *contentView;
    __weak IBOutlet UIView *batteryPercentView;
    __weak IBOutlet NSLayoutConstraint *percentHeightConstraint;
    __weak IBOutlet UIImageView *batteryBgImageView;
    //    __weak IBOutlet UILabel *distanceLabel;
    NSInteger batteryPercent;
    BOOL animate;
}

-(void)awakeFromNib
{
    
    [super awakeFromNib];
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil];
    
    [self addSubview:contentView];
    
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0]];
    batteryPercentView.hidden = YES;
}

//- (void)setState:(NIBatteryChargeState)state{
//    //充电中显示绿色
//    if(_charging){
//        chargeShadowImageView.image = [UIImage imageNamed:@"Charge_green_shadow"];
//        chargeBgImageView.image = [UIImage imageNamed:@"Charge_green_light"];
//        batteryPercentImageView.image = [UIImage imageNamed:@"Charge_container_colour_green"];
//    }else{
//        switch (state) {
//            case NIBatteryChargeStateLow:
//                chargeShadowImageView.image = [UIImage imageNamed:@"Charge_red_shadow"];
//                chargeBgImageView.image = [UIImage imageNamed:@"Charge_red_light"];
//                batteryPercentImageView.image = [UIImage imageNamed:@"Charge_container_colour_red"];
//                break;
//            case NIBatteryChargeStateMiddle:
//                chargeShadowImageView.image = [UIImage imageNamed:@"Charge_orange_shadow"];
//                chargeBgImageView.image = [UIImage imageNamed:@"Charge_orange_light"];
//                batteryPercentImageView.image = [UIImage imageNamed:@"Charge_container_colour_orange"];
//                break;
//
//            default:
//                chargeShadowImageView.image = [UIImage imageNamed:@"Charge_green_shadow"];
//                chargeBgImageView.image = [UIImage imageNamed:@"Charge_green_light"];
//                batteryPercentImageView.image = [UIImage imageNamed:@"Charge_container_colour_green"];
//                break;
//        }
//    }
//
//}

- (void)setBatteryPercent:(NSInteger)percent{
    batteryPercentView.hidden = NO;
    batteryPercent = percent;
    percentHeightConstraint.constant = batteryBgImageView.frame.size.height * percent / 100;
    [batteryPercentView setNeedsLayout];
    [contentView layoutIfNeeded];
}

//- (void)setDistance:(NSInteger)distance{
//    distanceLabel.text = [NSString stringWithFormat:@"%ld",(long)distance];
//}

- (void)startAnimation{
    if(animate){
        return;
    }
    batteryPercentView.hidden = NO;
    animate = YES;
    percentHeightConstraint.constant = 0;
    
    [self layoutIfNeeded];
    [UIView animateWithDuration:3.0f animations:^{
        [self setBatteryPercent:batteryPercent];
    } completion:^(BOOL finished) {
        if (animate) {
            animate = NO;
            [self startAnimation];
        }
    }];
}

- (void)stopAnimation{
    [self setBatteryPercent:batteryPercent];
    batteryPercentView.hidden = NO;
    animate = NO;
}

@end
